<template>
  <div class="app-container">
    <el-row>
      <el-form
        ref="queryForm"
        :model="queryParams"
        size="small"
        :inline="true"
        label-width="68px"
        @submit.native.prevent
      >
        <el-form-item label="" prop="name">
          <el-input
            v-model="queryParams.searchKey"
            placeholder="请输入关键字"
            clearable
            style="width: 240px"
            @clear="handleQuery"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
          >查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="mini" icon="el-icon-refresh" @click="handleReset">重置 </el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-row>
      <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddList">新建</el-button>
      <el-button :disabled="!ids.length" icon="el-icon-delete" size="mini" @click="handleDelList">删除</el-button>
    </el-row>
    <hk-table
      :loading="loading"
      :table-data="tableData"
      :table-columns="tableLabelLists"
      :selection="true"
      :total="total"
      :operate="true"
      :show-index="true"
      :query-params="queryParams"
      table-height="80"
      @handleSelectionChange="handleSelectionChange"
      @changePage="handlePage"
    >
      <template #operate="scope">
        <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
        <el-button type="text" @click="handleCopy(scope.row)">复制</el-button>
      </template>
    </hk-table>
  </div>
</template>

<script>
import { pageQuery, deleteNotice } from '@/api/basicConfig/notice.js'
import HkTable from '@/components/HkTable'
export default {
  components: { HkTable },
  beforeRouteLeave(to, from, next) {
    from.meta.keepAlive = false
    next()
  },
  data() {
    return {
      loading: false,
      queryParams: {
        size: 10,
        current: 1
      },
      tableData: [],
      tableLabelLists: [
        { label: '名称', param: 'noticeName' },
        { label: '应用服务数', param: 'serviceNumber' },
        { label: '描述', param: 'noticeDescription' },
        { label: '更新时间', param: 'updateTime' }
      ],
      title: '新建',
      total: 0,
      ids: []
    }
  },
  mounted() {
    this.getListData()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getListData()
    },
    /** 重置按钮操作 */
    handleReset() {
      this.queryParams = {
        current: 1,
        size: 10
      }
      this.resetForm('queryForm')
      this.handleQuery()
    },
    //  新建按钮
    handleAddList() {
      this.$router.push({
        path: window.prefix + '/bpmn/sla/slaEdit',
        query: { type: 'add' }
      })
    },
    // 删除按钮
    handleDelList() {
      const _this = this
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteNotice(_this.ids)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getListData()
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 点击分页
    handlePage(val) {
      this.queryParams.current = val.current
      this.queryParams.size = val.size
      this.getListData()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.noticeId)
    },
    // 获取列表数据
    getListData() {
      this.loading = true
      pageQuery(this.queryParams).then((res) => {
        this.tableData = res.list
        this.total = res.total
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    //  编辑
    handleEdit(row) {
      this.$router.push({
        path: window.prefix + '/bpmn/sla/slaEdit',
        query: { type: 'edit', id: row.noticeId }
      })
    },
    // 复制
    handleCopy(row) {
      this.$router.push({
        path: window.prefix + '/bpmn/sla/slaEdit',
        query: { type: 'copy', id: row.noticeId }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .targetTable{
  overflow: hidden;
}
::v-deep .el-form {
  .el-form-item--small.el-form-item {
    margin-bottom: 18px !important;
  }
}
</style>
